package com.briup.myblog.web.controller;

import com.briup.myblog.bean.basic.Article;
import com.briup.myblog.bean.ext.ArticleExt;
import com.briup.myblog.service.IArticleService;
import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "资讯信息模块")
@Slf4j
@RestController
@RequestMapping("/auth/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @ApiOperation(value = "修改资讯状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", required = true),
            //0-待审核，1-不通过，2-通过，3-推荐
            @ApiImplicitParam(name = "status", value = "状态", required = true),
    })
    @PutMapping("/changeStatus")
    public Result changeStatus(Integer id, Integer status) {
        System.out.println("控制类的id="+id+"，status="+status);
        String state = articleService.changeArticlStatus(id, status);
        return ResultUtil.success("修改成功，目前状态为：" + state);
    }

    @ApiOperation(value = "通过id删除资讯")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @DeleteMapping("/deleteById")
    public Result deleteArticleById(Integer id) {
        articleService.deleteArticleById(id);
        return ResultUtil.success("删除成功");
    }

    @ApiOperation(value = "通过id联合查询资讯")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @GetMapping("/findById")
    public Result findArticleJointById(Integer id) {
        ArticleExt articleExt = articleService.findArticleJointById(id);
        return ResultUtil.success(articleExt);
    }

    @ApiOperation(value = "分页获取所有推荐的资讯")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true),
    })
    @GetMapping("/page/query/recommend")
    public Result findArticleRecommend(Integer pageNum, Integer pageSize) {
        PageInfo<ArticleExt> pageInfo = articleService.findArticleByStatus(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }

    @ApiOperation(value = "分页查询所有资讯信息")
    @ApiImplicitParams({
            //标题要支持模糊查询
            @ApiImplicitParam(name = "title", value = "资讯标题", dataType = "String"),
            @ApiImplicitParam(name = "categoryId", value = "分类中栏目id", dataType = "Integer"),
            //0-待审核，1-不通过，2-通过，3-推荐
            @ApiImplicitParam(name = "status", value = "资讯状态", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true),
    })
    @GetMapping("/page/queryWithCategoryAndUser")
    public Result findAllArticle(String title, Integer categoryId, Integer status, Integer pageNum, Integer pageSize) {
        PageInfo<ArticleExt> pageInfo = articleService.changeFind(title, categoryId, status, pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }

    @ApiOperation(value = "保存或更新信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleCategoryId", value = "所属栏目id", required = true),
            @ApiImplicitParam(name = "articleContent", value = "资讯内容", required = true),
            @ApiImplicitParam(name = "articleCover", value = "咨询封面地址", required = true),
            @ApiImplicitParam(name = "articleId", value = "编号"),
            @ApiImplicitParam(name = "articlePublishTime", value = "咨询发布时间", defaultValue = "不用填写", required = true),
            @ApiImplicitParam(name = "articleReadTimes", value = "咨询阅读次数", required = true),
            @ApiImplicitParam(name = "articleStatus", value = "咨询状态", required = true),
            @ApiImplicitParam(name = "articleThumpUp", value = "咨询点赞数", required = true),
            @ApiImplicitParam(name = "articleTitle", value = "咨询题目", required = true),
            @ApiImplicitParam(name = "articleUserId", value = "咨询发布用户id", required = true),
    })
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdateArticle(Article article) {
        String str = articleService.changeInsertOrUpdate(article);
        return ResultUtil.success(str);
    }
}
